import sys

def load_flow(fname, load_time=0, load_length=1, load_direction=1, no_neg=False, offset=1460):
    '''
        load_time = 0 means don't load packet timestamp (the return value will be a list of ±length)
        load_length = 0 means masking the packet length to ±1
        no_neg, offset: add lengths with offset to ensure that they are all non-negative numbers
    '''
    flow = []

    try:
        f = open(fname, "r")
        lines = f.readlines()
        f.close()
        
        if len(lines) == 0:
            print("Empty cell:", fname)
            sys.exit(-1)
        
        start_time = float(lines[0].split('\t')[0])
        for li in lines:
            li = li.split("\t")
            p = int(li[1])
            if load_direction == 0:
                p = p if p >= 0 else -p
            if load_direction == 1 and no_neg:
                p += offset
            if load_length == 0:
                p = 1 if p > 0 else -1
            if load_time == 0:
                flow.append(p)
            else:
                t = float(li[0])
                flow.append((t - start_time, p))
    except:
        print ("Could not load", fname)
        sys.exit(-1)
    return flow